#include "Mario.h"
#include "Resource.h"
#include <d3dx9.h>

/**-----------------------------------------------------------

* Description:	Constructor 
* Method name:	Mario
* Parameters:	float x, float y, bool isAi
* Return value: 
---------------------------------------------------------*/ 
Mario::Mario(float x, float y, bool isAi)
{
	_sprLeft = new Sprite(ResourceMng::GetInst()->GetTexture(L"mario_left.png"), 30);
	_sprRight = new Sprite(ResourceMng::GetInst()->GetTexture(L"mario_right.png"), 30);

	_curSprite = _sprRight;

	_turnLeft = false;
	_isJumping = false;
	_isAi = isAi;

	_x = x;
	_y = y;

	_vx = 0;
	_vy = 0;
	
	if(_isAi){
		_vx = MARIO_VX;
	}

}


/**-----------------------------------------------------------

* Description:	Destructor 
* Method name:	~Mario
* Parameters:	 
* Return value: 
---------------------------------------------------------*/
Mario::~Mario(void)
{
	delete _sprLeft;
	delete _sprRight;
}


/**-----------------------------------------------------------

* Description:	Update the mario in time
* Method name:	Update
* Parameters:	int time
* Return value: void
---------------------------------------------------------*/
void Mario::Update(int time)
{
	
	//velocity
	_x += _vx * time;
	_y += _vy * time;

	if(_vx > 0)
	{
		_vx -= ACCEL_X * time;
		if(_vx < 0) _vx = 0;
	}
	else
	{
		_vx += ACCEL_X * time;
		if(_vx > 0) _vx = 0;
	}
	
	_vy += GRAVITY * time;
	if(_y >= BOTTOM - _sprRight->_texture->Height)
	{
		_vy = 0;
		_isJumping = false;
		_y = BOTTOM - _sprRight->_texture->Height - 1;
	}


	if(_isAi)
	{
		if(_vx > 0)
		{
			TurnRight();
		}
		else
		{
			TurnLeft();
		}

		if(_x >= _ScreenWidth - _sprRight->_texture->Width)
		{
			TurnLeft();
		}
		else if(_x <= 0)
		{
			TurnRight();
		}
		
	}
	else
	{
		if(_x >= _ScreenWidth - _sprRight->_texture->Width)
		{
			_x = _ScreenWidth - _sprRight->_texture->Width;
		}
		else if(_x <= 0)
		{
			_x = 0;
		}
	}

	

	//sprite
	if(_vx != 0)
		_curSprite->Update(time);
}


/**-----------------------------------------------------------

* Description:	Render the mario 
* Method name:	Render
* Parameters:	 
* Return value: void
---------------------------------------------------------*/
void Mario::Render(){
	_curSprite->Render((int)_x, (int)_y);
}


/**-----------------------------------------------------------

* Description:	Set the mario turn left
* Method name:	TurnLeft
* Parameters:	 
* Return value: void
---------------------------------------------------------*/
void Mario::TurnLeft(){
	_turnLeft = true;
	_curSprite = _sprLeft;
	_vx = - MARIO_VX;
}


/**-----------------------------------------------------------

* Description:	Set the mario turn right
* Method name:	TurnRight
* Parameters:	 
* Return value: void
---------------------------------------------------------*/
void Mario::TurnRight(){
	_turnLeft = false;
	_curSprite = _sprRight;
	
	_vx = MARIO_VX;
}


/**-----------------------------------------------------------

* Description:	Set the mario jump
* Method name:	Jump
* Parameters:	 
* Return value: void
---------------------------------------------------------*/
void Mario::Jump(){
	if(!_isJumping){
		_isJumping = true;
		_vy = - MARIO_VY;
	}
}
void Mario::reset()
{
	_sprRight->Reset();
	_sprLeft->ResetLeft();
}